package com.rockxin.cat.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fuiou.mobile.FyPay;
import com.fuiou.mobile.FyPayCallBack;
import com.fuiou.mobile.bean.MchantMsgBean;
import com.fuiou.mobile.util.AppConfig;
import com.rockxin.cat.R;
import com.rockxin.cat.base.BaseActivity;
import com.rockxin.cat.base.User;
import com.rockxin.cat.bean.RechargeBean;
import com.rockxin.cat.utils.JumpActivityManager;
import com.rockxin.cat.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 富友支付
 */
public class GoFyPayActivity extends BaseActivity {

    @BindView(R.id.back_img)
    ImageView backImg;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.titleContainer)
    RelativeLayout titleContainer;
    @BindView(R.id.uid)
    TextView uid;
    @BindView(R.id.bank)
    TextView bank;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.orderNum)
    TextView orderNum;
    @BindView(R.id.idCard)
    TextView idCard;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.pay)
    TextView pay;

    private String partner;
    private String key;
    private RechargeBean rechargeBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FyPay.init(this);
        FyPay.setDev(false);
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_go_fy_pay;
    }

    @Override
    public void initData() {
        title.setText("富友支付");
        getBundle();

    }

    private void getBundle() {
        final Bundle extras = getIntent().getExtras();
        if (extras != null) {
            rechargeBean = extras.getParcelable("RechargeBean");
            bank.setText(rechargeBean.getPayinfo().getBank_card());
            money.setText(rechargeBean.getPayinfo().getMoney() + "");
            uid.setText(rechargeBean.getPayinfo().getUid());
            orderNum.setText(rechargeBean.getOrderid());
            idCard.setText(rechargeBean.getPayinfo().getIdno());
            name.setText(rechargeBean.getPayinfo().getReal_name());

        }
    }

    @OnClick({R.id.back_img, R.id.pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_img:
                finish();
                break;
            case R.id.pay:
                MchantMsgBean bean = new MchantMsgBean();
                bean.setOrderId(rechargeBean.getOrderid());
                bean.setKey(rechargeBean.getFuyoudata().getKey());
                bean.setMchntCd(rechargeBean.getFuyoudata().getPartner());//设置商户号
                bean.setAmt(String.valueOf(rechargeBean.getPayinfo().getMoney()));
                bean.setUserId(rechargeBean.getPayinfo().getUid());
                bean.setCardNo(rechargeBean.getPayinfo().getBank_card());
                bean.setIDcardType("0");
                bean.setIDNo(rechargeBean.getPayinfo().getIdno());
                bean.setUserName(rechargeBean.getPayinfo().getReal_name());
                bean.setBackUrl(rechargeBean.getNotify_url());
                bean.setPayType("mobilePay");

                FyPay.pay(this, bean, new FyPayCallBack() {

                    @Override
                    public void onPayComplete(String rspCode, String rspDesc, Bundle extraData) {
                        // rspCode: 0001（唯一）；
                        // rspDesc：用户取消支付（唯一）；
                        // extraData：支付传递的参数。
                        // 考虑不同的商户ui设计的不同，所以这里商户自行根据响应对界面做成功或者失败的展示
                        Log.e("http", "rspCode = " + rspCode + " ; rspDesc = " + rspDesc);

                    }

                    @Override
                    public void onPayBackMessage(
                            String paramString) {
                        // 支付返回处理，商户根据返回的paramString（参数参考表3.3.2）自行解析做界面展示，这里不再做技术解析说明！
                        Log.e("http", "onPayBackMessage >>>" + paramString);
                        if (paramString.contains("成功")) {
                            /*startActivity(new Intent(GoFyPayActivity.this, RechargeSuccessActivity.class));*/
                            Intent intent = new Intent(GoFyPayActivity.this, RechargeSuccessActivity.class);
                            intent.putExtra("fee", rechargeBean.getFee());
                            intent.putExtra("money", rechargeBean.getPayinfo().getMoney());
                            startActivity(intent);
                        } else {
                            ToastUtils.showShortToast("充值失败");
                        }


                    }
                });
                break;

        }
    }
}
